package com.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

import java.beans.PropertyVetoException;
import java.sql.SQLException;

//import org.springframework.jdbc.datasource.DataSourceTransactionManager;
//import org.springframework.transaction.support.TransactionTemplate;

/**
 * 和连接数据库相关的配置类
 */
//@PropertySource("jdbc.properties")
public class JdbcConfig {
	@Value("${mysql.driver}")//表示从对应的properties文件中读取对应的变量并赋值
	private String driverClass;

	@Value("${mysql.url}")
	private String jdbcUrl;

	@Value("${mysql.username}")
	private String user;

	@Value("${mysql.password}")
	private String password;



	/**
	 * 创建数据源对象
	 * 
	 * @return
	 * @throws PropertyVetoException
	 */
	@Bean(name = "dataSource")
	public ComboPooledDataSource createDataSource() throws PropertyVetoException, SQLException {
		System.out.println("dataSource init-------------------");
		ComboPooledDataSource ds = new ComboPooledDataSource();
		ds.setDriverClass(driverClass);
		ds.setJdbcUrl(jdbcUrl);
		ds.setUser(user);
		ds.setPassword(password);
		ds.setAutoCommitOnClose(false);
		ds.getConnection().setAutoCommit(false);
		return ds;
	}


}
